<?php

declare(strict_types=1);

namespace ImiApp\WebSocketServer\Controller;


use Imi\ConnectionContext;
use Imi\Controller\WebSocketController;
use Imi\Server\WebSocket\Route\Annotation\WSRoute;
// use Imi\Server\WebSocket\Route\Annotation\WSConfig;
use Imi\Server\WebSocket\Route\Annotation\WSAction;
use Imi\Server\WebSocket\Route\Annotation\WSController;

use Imi\Config;
use Imi\Db\Db;

/**
 * 系统消息
 * @WSController
 */
class System extends WsBase
{

    /**
     * 心跳
     *
     * @WSAction
     * @WSRoute({"action"="ping"})
     *
     * @param mixed $data
     */
    public function ping($data): array
    {
        $address = $this->frame->getClientAddress();
        // $message = '[' . $address->getAddress() . ':' . $address->getPort() . ']: ' ;
        return [
            'code' => 1,
            'type' => 'ping',
            'ip' => $address->getAddress(),
            // 'mode'    => \Imi\App::getApp()->getType(),
        ];
    }


    /**
     * 加入系统房间
     *
     * @WSAction
     * @WSRoute({"action"="system.join"})
     * @param mixed $data
     * @return void
     */
    public function join($data)
    {

        $user_id = ConnectionContext::get('user_id');
        if (!isset($data['auction_id'])) {
            return ['code' => -1, 'msg' => '缺少拍卖参数'];
        }
        $auction_id = $data['auction_id'];
        $群组名 =  'auction_' . $auction_id;
        $auction = Db::name('auction')->where('id', '=', $auction_id)->find();
        if (!$auction) {
            return $this->error('没有该拍卖');
        }

        $this->server->joinGroup($群组名, $this->frame->getClientId());

        $this->server->groupCall($群组名, 'push', $this->success('用户' . $user_id . '加入成功', [], 'system_join'));

        return $this->success('加入成功', ['auction' => $auction, 'count' => $this->server->getGroup($群组名)->count()]);
    }



    /**
     * 离开拍卖群
     *
     * @WSAction
     * @WSRoute({"action"="system.leave"})
     * @param mixed $data
     * @return void
     */
    public function leave($data)
    {
        $user_id = ConnectionContext::get('user_id');
        if (!isset($data['auction_id'])) {
            return ['code' => -1, 'msg' => '缺少拍卖参数'];
        }
        $auction_id = $data['auction_id'];
        $群组名 =  'auction_' . $auction_id;
        $res = $this->server->leaveGroup($群组名, $this->frame->getClientId());
        return $this->success('离开成功', ['res' => $res]);
    }


    /**
     * 加入拍卖列表
     *
     * @WSAction
     * @WSRoute({"action"="system.joinlist"})
     *
     * @param mixed $data
     */
    public function joinlist($data): array
    {

        $this->server->joinGroup(WS_拍卖列表, $this->frame->getClientId());
        return $this->success('拍卖列表', ['count' => $this->server->getGroup(WS_拍卖列表)->count()]);
    }

    /**
     * 离开拍卖列表
     *
     * @WSAction
     * @WSRoute({"action"="system.leavelist"})
     *
     * @param mixed $data
     */
    public function leavelist($data): array
    {
        $this->server->leaveGroup(WS_拍卖列表, $this->frame->getClientId());
        return $this->success('离开拍卖列表');
    }
}
